<?php

class Mithos_Account {
    private $_guid;
    private $_name;
    private $_username;
    private $_password;
    private $_email;
    private $_phone;
    private $_question;
    private $_answer;
    private $_block;
    private $_personalId;
    private $_golds;
    private $_characters = array();

    public function __construct($username = null) {
        if ($username !== null) {
            $this->read($username);
        }
    }

    public function name($name = null) {
        if ($name === null) {
            return $this->_name;
        }
        $this->_name = $name;
        return $this;
    }

    public function username($username = null) {
        if ($username === null) {
            return $this->_username;
        }
        $this->_username = $username;
        return $this;
    }

    public function password($password = null) {
        if ($password === null) {
            return $this->_password;
        }
        $this->_password = $password;
        return $this;
    }

    public function email($email = null) {
        if ($email === null) {
            return $this->_email;
        }
        $this->_email = $email;
        return $this;
    }

    public function phone($phone = null) {
        if ($phone === null) {
            return $this->_phone;
        }
        $this->_phone = $phone;
        return $this;
    }

    public function guid($guid = null) {
        if ($guid === null) {
            return $this->_guid;
        }
        $this->_guid = $guid;
        return $this;
    }

    public function question($question = null) {
        if ($question === null) {
            return $this->_question;
        }
        $this->_question = $question;
        return $this;
    }

    public function answer($answer = null) {
        if ($answer === null) {
            return $this->_answer;
        }
        $this->_answer = $answer;
        return $this;
    }

    public function block($block = null) {
        if ($block === null) {
            return $this->_block;
        }
        $this->_block = $block;
        return $this;
    }

    public function personalId($personalId = null) {
        if ($personalId === null) {
            return $this->_personalId;
        }
        $this->_personalId = $personalId;
        return $this;
    }

    public function data($field = null) {
        if ($field === null) {
            return $this->_data;
        }
        return $this->_data[$field];
    }
    
    public function golds($golds = null) {
        if ($golds === null) {
            $config = new Zend_Config(require CONFIG_DATABASE_FILE);
            $result = Mithos_Mssql::getInstance()->fetch('SELECT ' . $config->golds->column_golds . ' AS golds 
                FROM ' . $config->golds->table . ' 
                WHERE ' . $config->golds->column_account . ' = :username[string]
            ', array('username' => $this->username()));
            return $result['golds'];
        }
        $this->_golds = $golds;
        return $this;
    }

    public function isConnected() {
        return isset($this->_data['connect_stat']) && $this->_data['connect_stat'] == 1;
    }

    public function characters() {
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT 
            Name AS name
            FROM Character 
            WHERE AccountID = :username[string]
        ', array('username' => $this->username()));
        foreach ($results as $result) {
            $this->_characters[] = new Mithos_Character($result['name']);
        }
        return $this->_characters;
    }

    public function lastConnections() {
        $result = Mithos_Mssql::getInstance()->fetchAll('SELECT 
            ac.GameIDC AS character,
            ms.IP AS ip,
            ms.ConnectStat AS connect_stat,
            ms.ServerName AS server_name,
            ms.ConnectTM AS connected_at,
            ms.DisConnectTM AS disconnected_at
            FROM MEMB_STAT ms
            JOIN AccountCharacter ac ON ms.memb___id = ac.Id
            WHERE memb___id = :username[string] 
            ORDER BY ConnectTM DESC
        ', array('username' => $this->username()));
        return $result;
    }

    public function count($where = null) {
        $where = $where !== null ? ' WHERE ' . $where : '';
        $result = Mithos_Mssql::getInstance()->fetch('SELECT 
            COUNT(1) AS total FROM MEMB_INFO
        ' . $where);
        return $result['total'];
    }

    public function read($username) {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT
            mi.memb_guid AS guid,
            mi.memb___id AS username,
            mi.memb__pwd AS password,
            mi.memb_name AS name,
            mi.mail_addr AS email,
            mi.tel__numb AS phone,
            mi.fpas_ques AS question,
            mi.fpas_answ AS answer,
            mi.sno__numb AS personal_id,
            mi.bloc_code AS block,
            ms.ServerName AS server_name,
            ms.ConnectTM AS connected_at,
            ms.DisConnectTM AS disconnected_at,
            ms.ConnectStat AS connect_stat,
            ms.IP AS ip,
            ac.GameIDC AS last_used_character
            FROM MEMB_INFO mi
            LEFT JOIN MEMB_STAT ms ON mi.memb___id = ms.memb___id COLLATE DATABASE_DEFAULT
            LEFT JOIN AccountCharacter ac ON mi.memb___id = ac.Id
            WHERE mi.memb___id = :username[string]
        ', array('username' => $username));
        $this->_data = $result;
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                if (method_exists($this, Mithos_Util_Text::camelize($key))) {
                    $this->{Mithos_Util_Text::camelize($key)}($value);
                }
            }
        }
    }

    public function insert() {
        $params = array(
            'guid' => $this->_nextGuid(),
            'username' => $this->username(),
            'password' => $this->password(),
            'name' => $this->name(),
            'email' => $this->email(),
            'phone' => $this->phone(),
            'question' => $this->question(),
            'answer' => $this->answer(),
            'block' => $this->block(),
            'personal_id' => $this->personalId()
        );

        Mithos_Mssql::getInstance()->query('SET IDENTITY_INSERT MEMB_INFO ON; INSERT INTO MEMB_INFO 
            (memb_guid, memb___id, memb__pwd, memb_name, sno__numb, tel__numb, 
            mail_addr, fpas_ques, fpas_answer, job_code, appl_days, madi_days,
            out__days, true_days, mail_check, bloc_code, ctl1_code) VALUES 
            (:username[string], :password[string], :name[string], :personal_id[string], :phone[string], :email[string],
            :question[string], :answer[string], 1, GETDATE(), GETDATE(),
            GETDATE(), GETDATE(), 1, :block[boolean], 1); SET IDENTITY_INSERT MEMB_INFO OFF;
        ', $params);
    }

    public function update() {
        $params = array(
            'username' => $this->username(),
            'password' => $this->password(),
            'name' => $this->name(),
            'email' => $this->email(),
            'phone' => $this->phone(),
            'question' => $this->question(),
            'answer' => $this->answer(),
            'block' => $this->block(),
            'personal_id' => $this->personalId()
        );

        Mithos_Mssql::getInstance()->query('UPDATE MEMB_INFO SET 
            memb__pwd = :password[string],
            memb_name = :name[string],
            mail_addr = :email[string],
            fpas_ques = :question[string],
            fpas_answ = :answer[string],
            bloc_code = :block[boolean],
            tel__numb = :phone[string],
            sno__numb = :personal_id[string]
            WHERE memb___id = :username[string]
        ', $params);
    }

    public function delete() {
        
    }

    private function _nextGuid() {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT MAX(memb_guid)+1 AS next FROM MEMB_INFO');
        return $result['next'] + 1;
    }
}

?>